<template>
  <div class="popupMain">
    <p class="title">写评论</p>
    <textarea name="commit" id="commit" cols="30" rows="10" :maxlength="200" v-model="textValue" placeholder="我有话想说..."  autofocus></textarea>
    <div class="btn">
      <button :disabled="commitDisabled" @click="sendCommit">提交评论</button>
    </div>
  </div>
</template>

<script>
import api from "../api";
import {toast} from "../toast";
export default {
  data() {
    return {
      textValue: '',
      commitDisabled: false,
    }
  },
  mounted(){
      //获取token给APP用的，这边不需要调用
      let that=this;
      window.updateToken = function (token, mobile) {
          that.token = token;
          that.mobile = mobile;
          // 用户已登录
      }
      // that.token = "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhcHAxNzQ1Njg0IiwidXNlcklkIjoiMTc0NTY4NCIsImZyb20iOiJhcHAiLCJuYmYiOjE1NjExODY2MDJ9.Oegm5Gimj50xym2nLAd0CLBFGwg7ZTxtg3dkRkMk7s4sMm4IJQT_qyTCmHfJQdAB8KvaeAh8svFBYqSb1a3Ht2M6NBrRAge-tI8UYh3Px-pue1fH1qS9xCbmqQTaTaLDiSQOl_g-zLyULF7zErIvd3xne65IApb52LhsVQ5gSTo";
  },
  created() {
			let that=this;
			if(that.isTerminal()=='isiOS'){
			    window.webkit.messageHandlers.getToken.postMessage({})
			}else{
			   ZMParentsJsToAndroid.getToken()
			};
	},
  watch: {
		textValue(string) {
			if (string.length > 0) {
				this.commitDisabled = false;
			} else {
				this.commitDisabled = true;
			}
		}
  },
  methods: {
    sendCommit() {
			
			if (this.textValue.length > 200) {
				toast('评论字数超过200字，请重新填写！');
			} else if (this.textValue.length == 0) {
				toast('评论不能为空，请重新填写！');
			} else {
				this.commitDisabled = true;
				api.infoTopicComment({
          topicId: this.$route.query.tid,
          content: this.textValue
				}, this.token).then(res => {
					if (res.data.code == '0') {
            // this.$router.push({path: '/informationDetail',query:{tid: this.$route.query.tid}});
            window.history.go(-1);
          }
          toast(res.data.message);

				}).catch(error => {
					toast(error);
				}).finally(() => {
				});
			}
			
    },
    //安卓,IOS方法
		isTerminal(){
      var u = navigator.userAgent;
      var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
      var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
      if (isAndroid) {
          return "isAndroid"
      } else if (isiOS) {
          return "isiOS"
      }
		},
  },
}
</script>

<style lang="scss">
@import "../styles/mixin.scss";
  .popupMain {
    padding: 0 px2rem(32);
    padding-top: px2rem(30);

    .title {
      color: #222;
      font-size: px2rem(36);
      line-height: px2rem(40);
      font-weight: bold;
    }

    #commit {
      margin-top: px2rem(32);
      width: 100%;
      padding: px2rem(25) px2rem(24);
      height: px2rem(320);
      background: rgba(247,247,250,1);
      border-radius: px2rem(10);
      border: none;
      box-sizing: border-box;
      color: #222;
      font-size: px2rem(28);
      line-height: px2rem(36);
    }

    .btn {
      text-align: center;
      margin-top: px2rem(56);

      button {
        width: px2rem(320);
        height: px2rem(88);
        background: #FFAD0D;
        border: none;
        outline: none;
        border-radius: px2rem(44);
        color: #fff;
        font-size: px2rem(30);
        // font-weight: bold;
        letter-spacing: 1px;
      }

    }

  }
</style>
